import ElementPlus from 'element-plus'
import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import 'element-plus/dist/index.css'
import '@/assets/global.css'
import axios from "axios";

const app = createApp(App)

app.use(router)
app.use(ElementPlus)
app.mount('#app')
//创建axiosObject对象
const axiosObject  = axios.create({
    baseURL:"http://localhost:8080",
    timeout:10000
})

// 请求拦截器
axiosObject.interceptors.request.use(
    config => {
        const loginInfo = localStorage.getItem("loginAdmin")
        if(loginInfo==null)
            return config;
        const token=JSON.parse(loginInfo).token;
        if (token) {
            config.headers['token'] = token;
        }
        return config;
    },
    error => {
        return Promise.reject(error)
    }
)

//响应拦截器
axiosObject.interceptors.response.use(
    response => {
        if(response.data.code==401){
            localStorage.removeItem("loginAdmin");
            router.push("/login");
        }
        return response;
    },
    error => {
        return Promise.reject(error);
    }
)

export default axiosObject
